package com.escape.subject.domain.handler.subject;

import com.escape.subject.common.enums.IsDeletedEnum;
import com.escape.subject.common.enums.SubjectInfoTypeEnum;
import com.escape.subject.domain.convert.JudgeSubjectConverter;
import com.escape.subject.domain.entity.SubjectAnswerBO;
import com.escape.subject.domain.entity.SubjectInfoBO;
import com.escape.subject.domain.entity.SubjectOptionBO;
import com.escape.subject.infra.basic.entity.SubjectJudge;
import com.escape.subject.infra.basic.service.SubjectJudgeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: escape_zebra
 * @CreateTime: 2024-12-26  11:54
 * @Description: 判断题目策略类
 * @Version: 1.0
 */
@Component
public class JudgeTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectJudgeService subjectJudgeService;

    @Override
    public SubjectInfoTypeEnum getSubjectType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        // 1. 判空
        if (subjectInfoBO.getOptionList() == null) {
            return;
        }

        // 2. 获取选项
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectInfoBO.getOptionList().forEach(option -> {
            subjectJudge.setSubjectId(subjectInfoBO.getId());
            subjectJudge.setIsCorrect(option.getIsCorrect());
            subjectJudge.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        });

        // 3. 插入单选答案
        subjectJudgeService.insert(subjectJudge);
    }

    @Override
    public SubjectOptionBO query(SubjectInfoBO subjectInfoBO) {
        // 1. 封装查询类
        SubjectJudge judge = new SubjectJudge();
        judge.setSubjectId(subjectInfoBO.getId());
        judge.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());

        // 2. 查询判断答案
        List<SubjectJudge> subjectJudgeList = subjectJudgeService.batchQueryByCondition(judge);

        // 3. 转换 entity --> bo
        List<SubjectAnswerBO> subjectAnswerBOS = JudgeSubjectConverter.INSTANCE.convertEntityToBoList(subjectJudgeList);

        // 4. 封装 & 返回
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }
}
